package com.example.gzysmartcity.ui.fragment.idea;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentPersonnelBinding;
import com.example.gzysmartcity.databinding.PopuCleanBinding;
import com.example.gzysmartcity.repository.model.Personnel;
import com.example.gzysmartcity.ui.adapter.PersonnelRvAdapter;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.utils.PopupUntil;
import com.example.gzysmartcity.viewmodel.PersonnelViewModel;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class PersonnelFragment extends BaseFragment<FragmentPersonnelBinding> {
    @Override
    protected FragmentPersonnelBinding viewBinding() {
        return FragmentPersonnelBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getArguments();
        int typeId = bundle.getInt("PositiontypeId", 0);
        PersonnelViewModel personnelViewModel = new ViewModelProvider(this).get(PersonnelViewModel.class);
        personnelViewModel.getPersonnelData(typeId);
        personnelViewModel.getPersonnelLiveData().observe(getViewLifecycleOwner(), state -> {
            switch (state) {
                case SUCCESS:
//                    因为你是直接用了viewmodel的list，你clear就会清除原来的
                    List<Personnel.DataBean> personnelList = new ArrayList<>(personnelViewModel.getPersonnelList());
                    PersonnelRvAdapter adapter = new PersonnelRvAdapter(R.layout.rv_personnel);
                    adapter.setNewInstance(personnelList);
                    b.rvPresonnel.setAdapter(adapter);
                    b.rvPresonnel.setLayoutManager(new LinearLayoutManager(getContext()));
                    b.searchPresonnel.setOnCloseListener(() -> {
                        personnelList.clear();
                        personnelList.addAll(personnelViewModel.getPersonnelsSreach(""));
                        adapter.notifyDataSetChanged();
                        return false;
                    });
                    b.searchPresonnel.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                        @Override
                        public boolean onQueryTextSubmit(String query) {
                            b.searchPresonnel.setIconified(true);
                            personnelList.clear();
                            personnelList.addAll(personnelViewModel.getPersonnelsSreach(query));
                            adapter.notifyDataSetChanged();
                            return false;
                        }

                        @Override
                        public boolean onQueryTextChange(String newText) {
                            return false;
                        }
                    });
                    adapter.addChildClickViewIds(R.id.btn_personnel_zp);
                    adapter.setOnItemChildClickListener((adapter1, view1, position) -> {
                        if (view1.getId() == R.id.btn_personnel_zp) {
                            PopupUntil popupUntil = new PopupUntil();
                            PopuCleanBinding popuCleanBinding = PopuCleanBinding.inflate(getLayoutInflater());
                            popupUntil.getPopupDataByView(popuCleanBinding.getRoot(), requireContext());
                            popuCleanBinding.tvCleanTitle.setText("发送应聘信息");
                            popuCleanBinding.tvCleanContent.setText("将以邮箱的方式发送信息给招\n" +
                                    "聘者，前去贵公司应聘");
                            popuCleanBinding.tvCleanNo.setOnClickListener(v1 -> popupUntil.closePopup());
                            popuCleanBinding.tvCleanYes.setOnClickListener(v1 -> {
                                ToastUtils.showShort("已短信下发通知");
                                popupUntil.closePopup();
                            });
                        }
                    });
                    break;
                case FAIL:
                    break;
            }
        });
    }

    @Override
    protected String setTitle() {
        return "应聘人才";
    }

    @Override
    protected boolean showBack() {
        return true;
    }

    @Override
    protected Boolean showBottom() {
        return false;
    }


}
